package com.open.common.util;

import java.security.MessageDigest;

public class MD5Utils {
	private static String toHexString(byte[] b) {
		char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		StringBuilder sb = new StringBuilder(b.length * 2);
		for (int i = 0; i < b.length; i++) {
			sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
			sb.append(HEX_DIGITS[b[i] & 0x0f]);
		}
		return sb.toString();
	}

	public static String getBit32(String str) {
		String result = "00000000000000000000000000000000";
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(str.getBytes());
			byte messageDigest[] = digest.digest();
			result = toHexString(messageDigest);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String getBit16(String str) {
		return getBit32(str).substring(8, 24);
	}
}
